/*
 * This file is part of aion-lightning <aion-lightning.org>.
 * 
 * aion-lightning is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * aion-lightning is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with aion-lightning.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.aionengine.gameserver.model.instance.instancereward;

import com.aionengine.gameserver.model.autogroup.AGPlayer;
import com.aionengine.gameserver.model.gameobjects.player.Player;
import com.aionengine.gameserver.model.instance.playerreward.HarmonyGroupReward;
import com.aionengine.gameserver.model.instance.playerreward.InstancePlayerReward;
import com.aionengine.gameserver.network.aion.serverpackets.SM_INSTANCE_SCORE;
import com.aionengine.gameserver.utils.PacketSendUtility;
import com.aionengine.gameserver.world.WorldMapInstance;
import com.aionengine.gameserver.world.knownlist.Visitor;
import javolution.util.FastList;

import java.util.Comparator;
import java.util.List;

import static ch.lambdaj.Lambda.*;

/**
 * @author xTz
 */
public class HarmonyArenaReward extends PvPArenaReward {

    private FastList<HarmonyGroupReward> groups = new FastList<HarmonyGroupReward>();

    public HarmonyArenaReward(Integer mapId, int instanceId, WorldMapInstance instance) {
        super(mapId, instanceId, instance);
    }

    public HarmonyGroupReward getHarmonyGroupReward(Integer object) {
        for (InstancePlayerReward reward : groups) {
            HarmonyGroupReward harmonyReward = (HarmonyGroupReward) reward;
            if (harmonyReward.containPlayer(object)) {
                return harmonyReward;
            }
        }
        return null;
    }

    public FastList<HarmonyGroupReward> getHarmonyGroupInside() {
        FastList<HarmonyGroupReward> harmonyGroups = new FastList<HarmonyGroupReward>();
        for (HarmonyGroupReward group : groups) {
            for (AGPlayer agp : group.getAGPlayers()) {
                if (agp.isInInstance()) {
                    harmonyGroups.add(group);
                    break;
                }
            }
        }
        return harmonyGroups;
    }

    public FastList<Player> getPlayersInside(HarmonyGroupReward group) {
        FastList<Player> players = new FastList<Player>();
        for (Player playerInside : instance.getPlayersInside()) {
            if (group.containPlayer(playerInside.getObjectId())) {
                players.add(playerInside);
            }
        }
        return players;
    }

    public void addHarmonyGroup(HarmonyGroupReward reward) {
        groups.add(reward);
    }

    public FastList<HarmonyGroupReward> getGroups() {
        return groups;
    }

    public void sendPacket(final int type, final Integer object) {
        instance.doOnAllPlayers(new Visitor<Player>() {

            @Override
            public void visit(Player player) {
                PacketSendUtility.sendPacket(player, new SM_INSTANCE_SCORE(type, getTime(), getInstanceReward(), object));
            }

        });
    }

    @Override
    public int getRank(int points) {
        int rank = -1;
        for (HarmonyGroupReward reward : sortGroupPoints()) {
            if (reward.getPoints() >= points) {
                rank++;
            }
        }
        return rank;
    }

    public List<HarmonyGroupReward> sortGroupPoints() {
        return sort(groups, on(HarmonyGroupReward.class).getPoints(), new Comparator<Integer>() {

            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 != null ? o2.compareTo(o1) : -o1.compareTo(o2);
            }

        });
    }

    @Override
    public int getTotalPoints() {
        return sum(groups, on(HarmonyGroupReward.class).getPoints());
    }

    @Override
    public void clear() {
        groups.clear();
        super.clear();
    }

}
